import logging
from flask import Flask
from flask_cors import CORS
import tornado.wsgi
import tornado.httpserver
from tornado.ioloop import IOLoop
import configparser
import os
import sys
# 蓝图导入
from test_page import test_page
from service_page import service_page
from ts_rechcek_box_parse_exception_page import ts_rechcek_box_parse_exception_page
from ts_recheck_all_detail_pre_page import ts_recheck_all_detail_pre_page
from ts_recheck_config_page import ts_recheck_config_page
from ts_recheck_erp_res_page import ts_recheck_erp_res_page
from ts_recheck_res_page import ts_recheck_res_page
from ts_recheck_temp_res_page import ts_recheck_temp_res_page
from ts_recheck_user_page import ts_recheck_user_page
# from public_listen_page import public_listen_page
from select_public_binfile_save_page import select_public_binfile_save_page

app = Flask(__name__)
CORS(app, supports_credentials=True)


# 创建配置文件解析器
config = configparser.ConfigParser()
# 读取配置文件
os.chdir(os.path.dirname(os.path.abspath(sys.argv[0])))
config.read('configs.ini', encoding='utf-8-sig')

port = config['port']['port']

# 配置文件
app.config['PORT'] = port
# 注册蓝图
app.register_blueprint(test_page)
app.register_blueprint(service_page)
app.register_blueprint(ts_rechcek_box_parse_exception_page)
app.register_blueprint(ts_recheck_all_detail_pre_page)
app.register_blueprint(ts_recheck_config_page)
app.register_blueprint(ts_recheck_erp_res_page)
app.register_blueprint(ts_recheck_res_page)
app.register_blueprint(ts_recheck_temp_res_page)
app.register_blueprint(ts_recheck_user_page)
# app.register_blueprint(public_listen_page)
app.register_blueprint(select_public_binfile_save_page)

# 创建 Tornado WSGI 应用
wsgi_app = tornado.wsgi.WSGIContainer(app)

# 创建 Tornado HTTP 服务器并绑定到指定端口
http_server = tornado.httpserver.HTTPServer(wsgi_app)
http_server.listen(app.config['PORT'], address="0.0.0.0")
print(f'服务器已启动，端口号：{app.config["PORT"]}')
# 启动 Tornado 事件循环
IOLoop.current().start()
